package com.sakura.mall.coupon.skuLadder.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.coupon.skuLadder.domain.entity.SkuLadderEntity;
import com.sakura.mall.coupon.skuLadder.domain.request.AddSkuLadderRequest;
import com.sakura.mall.coupon.skuLadder.domain.request.UpdateSkuLadderRequest;
import com.sakura.mall.coupon.skuLadder.domain.request.SkuLadderPageRequest;
import com.sakura.mall.coupon.skuLadder.domain.vo.SkuLadderVo;
import com.sakura.mall.coupon.skuLadder.mapper.SkuLadderMapper;
import com.sakura.mall.coupon.skuLadder.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品阶梯价格 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderMapper, SkuLadderEntity> implements SkuLadderService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSkuLadderRequest request) {
        // todo 其他校验逻辑
        SkuLadderEntity skuLadder = request.convert(SkuLadderEntity.class);
        this.save(skuLadder);
    }

    @Override
    public Page<SkuLadderVo> queryPage(SkuLadderPageRequest request) {
        IPage<SkuLadderEntity> qp = request.toPage();
        LambdaQueryWrapper<SkuLadderEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SkuLadderVo> ret = qp.convert(entity -> entity.convert(SkuLadderVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSkuLadderRequest request) {
        SkuLadderEntity entity = request.convert(SkuLadderEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

